package cn.hebutrun.oulabot;

import cn.hebutrun.oulabot.entity.BaseEntity;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * @author zhaolong
 * @since 2022-09-07
 */
@Component
public class MybatisPlusGenerator {

  @Value("${spring.datasource.druid.url}")
  private String url;

  @Value("${spring.datasource.druid.username}")
  private String username;

  @Value("${spring.datasource.druid.password}")
  private String pwd;

  private static final List<String> tableList = List.of("oula_file");

  public void generate() {
    FastAutoGenerator.create(
            url,
            username,
            pwd)
        .globalConfig(
            builder ->
                builder
                    .author("Adai") // 设置作者
                    .enableSwagger() // 开启 swagger 模式
                    .outputDir(System.getProperty("user.dir") + "/src/main/java") // 指定输出目录
                    .commentDate("yyyy-MM-dd")
                    .disableOpenDir())
        .packageConfig(
            builder ->
                builder
                    .parent("cn.hebutrun.oulabot") // 设置父包名
                    //              .moduleName("system") // 设置父包模块名
                    .pathInfo(
                        Collections.singletonMap(
                            OutputFile.xml,
                            System.getProperty("user.dir")
                                + "/src/main/resources/mapper/")) // 设置mapperXml生成路径
            )
        .strategyConfig(
            builder ->
                builder
                    .addInclude(tableList) // 设置需要生成的表名
                    //                    .addTablePrefix("t_", "c_") // 设置过滤表前缀
                    .entityBuilder()
                    .enableLombok()
                    .enableChainModel()
                    .enableRemoveIsPrefix()
                    .logicDeleteColumnName("is_del")
                    .superClass(BaseEntity.class)
                    .addSuperEntityColumns("id", "is_del", "note", "create_time", "update_time")
                    .controllerBuilder()
                    .enableRestStyle()
                    .mapperBuilder()
                    .enableBaseResultMap())
        .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
        .execute();
  }
}
